//
//  TMTeacherDiscriptCell.m
//  国学商家管理
//
//  Created by Tao Li on 2018/7/5.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMTeacherDiscriptCell.h"
#import "TMCourseDetailModel.h"
#import <CoreText/CoreText.h>
@implementation TMTeacherDiscriptCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.headerImage.contentMode = UIViewContentModeScaleAspectFill;
    self.headerImage.clipsToBounds = YES;
}
- (IBAction)moreBtnClick:(UIButton *)sender {
    sender.hidden = NO;
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self.moreBtn setTitle:@"更多" forState:UIControlStateNormal];
    }else{
         [self.moreBtn setTitle:@"收起" forState:UIControlStateNormal];
    }
    if (self.showBlock) {
        self.showBlock(sender.selected);
    }
}
-(void)setModel:(TMCourseDetailModel *)model{
    _model = model;
    [self.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithBaseUrl:model.userimage]] placeholderImage:kPlaceholder];
    self.teacherName.text = model.username;
    NSString *ss = @"我女被 v 你是 v 你说的 v 山东女村地处 v 不都是 v 白色的 v 呢cndvnsdvnxc.vnbdjkfvsdfvnxbvsdku蚕丝的腹背受敌 v 都是 v 你说的比女蚕丝DVD绿坝娘苏打绿技能熟练的肌肤 i 额外我女被 v 你是 v 你说的 v 山东女村地处 v 不都是 v 白色的 v 呢cndvnsdvnxc.vnbdjkfvsdfvnxbvsdku蚕丝的腹背受敌 v 都是 v 你说的比女蚕丝DVD绿坝娘苏打绿技能熟练的肌肤 i 额外";//model.abstract
    //model.abstract = ss;
    self.contentLabel.text = model.abstract;
    NSLog(@"model.abstract----------%@", model.abstract);
    self.moreBtn.selected = model.show;
    if (!model.show) {
        [self.moreBtn setTitle:@"更多" forState:UIControlStateNormal];
    }else{
        [self.moreBtn setTitle:@"收起" forState:UIControlStateNormal];
    }
    if (self.contentLabel.text != nil) {
        NSArray *lineCount = [self getLinesArrayOfStringInLabel:self.contentLabel];
        if (lineCount.count >2 && model.show==NO) {
            self.contentLabel.text =[NSString stringWithFormat:@"%@%@", lineCount[0],lineCount[1]];
        }else{
            self.contentLabel.text = model.abstract;
            self.moreBtn.hidden = YES;
        }
        if (lineCount.count >2) {
            self.moreBtn.hidden= NO;
            self.rightc.constant = 45;
        }else{
            self.moreBtn.hidden= YES;
            self.rightc.constant = 20;
        }
    }

}

- (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label{
    NSString *text = [label text];
    UIFont *font = [label font];
    CGRect rect = [label frame];
    
    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)myFont range:NSMakeRange(0, attStr.length)];
    CFRelease(myFont);
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];
    for (id line in lines) {
        CTLineRef lineRef = (__bridge  CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        NSString *lineString = [text substringWithRange:range];
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));
        //NSLog(@"''''''''''''''''''%@",lineString);
        [linesArray addObject:lineString];
    }
    
    CGPathRelease(path);
    CFRelease( frame );
    CFRelease(frameSetter);
    return (NSArray *)linesArray;
}


@end
